# Contributor: Carlo Landmeter <clandmeter@gmail.com>
# Contributor: Chloe Kudryavtsev <toast@toastin.space>
# Maintainer: Michał Polański <michal@polanski.me>
pkgname=caddy1
_pkgname=caddy
pkgver=1.0.5
pkgrel=0
pkgdesc="Fast, cross-platform HTTP/2 web server with automatic HTTPS"
url="https://caddyserver.com/"
arch="all !s390x !mips !mips64" # limited by go
license="Apache-2.0"
# TestVisibleErrorWithPanic fails
options="net !check"
# tests fail on x86 builders due to containerization edge-cases
case $CARCH in
	x86) options="$options !check";;
esac
depends="ca-certificates"
makedepends="go libcap bash"
subpackages="$pkgname-openrc"
install="$pkgname.pre-install"
pkgusers="$_pkgname"
pkggroups="$_pkgname"
source="$_pkgname-$pkgver.tar.gz::https://github.com/caddyserver/caddy/archive/v$pkgver.tar.gz
	$pkgname.initd
	$pkgname.confd
	$_pkgname.conf
	"
builddir="$srcdir/src/github.com/mholt/$_pkgname"

export GOPATH="$srcdir"

prepare() {
	mkdir -p ${builddir%/*}
	mv "$srcdir"/$_pkgname-$pkgver "$builddir"/
	default_prepare
}

build() {
	GO111MODULE=on go build -v -o bin/caddy -ldflags \
		"-X github.com/mholt/caddy/caddy/caddymain.gitTag=$pkgver" \
		./caddy
}

check() {
	GO111MODULE=on go test ./...
}

package() {
	install -Dm755 bin/caddy "$pkgdir"/usr/sbin/$pkgname

	# caddy currently does not support dropping privileges so we
	# change attributes with setcat to allow access to priv ports
	# https://caddyserver.com/docs/faq
	setcap cap_net_bind_service=+ep \
		"$pkgdir"/usr/sbin/$pkgname

	install -d -o caddy -g caddy \
		"$pkgdir"/var/lib/$pkgname \
		"$pkgdir"/etc/$pkgname \
		"$pkgdir"/var/www

	install -Dm755 "$srcdir"/$pkgname.initd \
		"$pkgdir"/etc/init.d/$pkgname
	install -Dm644 "$srcdir"/$pkgname.confd \
		"$pkgdir"/etc/conf.d/$pkgname
	install -Dm644 "$srcdir"/$_pkgname.conf \
		"$pkgdir"/etc/$pkgname/$_pkgname.conf
}

cleanup_srcdir() {
	go clean -modcache
	default_cleanup_srcdir
}

sha512sums="6f6df0b5bfc60cb6d67e6e65ddee7f16d12ae4c80f0168383807490f4d519e04cfdc754acf8b68d7996d52a764d4b13b5ce710cd391f8287a9907e0bec317ce1  caddy-1.0.5.tar.gz
e009740cf7a5c4195d410ba26a703b808bf573a6b0fbd37f7a5a8aab918ccae572b9b0b1374475058a9a6f70cc6f7b025c3eb8b3a379dd78b1f3d6c6806eb5b1  caddy1.initd
5f3421986f37b1569c7cc4a47627510ac253aec7f2658903aca93c04c2e3f78288803085892de885c7d0e3753a4b29effabf3d4025366ff36ab0c030e314cd8d  caddy1.confd
c24805d17234e6cf40fe1dd102c03f05cf6129d43f58f5567d540a0e4400ce89994820bb0e317f611c65459ae26bcf7110e23a8fecaae11ca78a561892b45d75  caddy.conf"
